﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MS.Core.Constants.Objects;
using LHS.Core.TableInfo;

namespace LHS.Win
{
    public partial class frmBenhAnChuaTinhTien : Form
    {
        LHS.Core.Clinical.ClinicalController objConntroler = new LHS.Core.Clinical.ClinicalController();
        // Methods
        public frmBenhAnChuaTinhTien()
        {
            this.InitializeComponent();
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btFirst_Click(object sender, EventArgs e)
        {
            this.bmb.Position = 0;
        }

        private void btLast_Click(object sender, EventArgs e)
        {
            if (this.tbControl.SelectedIndex == 0)
            {
                DataSet dataSource = (DataSet)this.dataGrid1.DataSource;
                this.bmb.Position = dataSource.Tables[0].Rows.Count - 1;
            }
            else
            {
                DataSet set2 = (DataSet)this.dataGrid2.DataSource;
                this.bmb.Position = set2.Tables[0].Rows.Count - 1;
            }
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            string selectCommandText = string.Empty;
            if (this.tbControl.SelectedIndex == 0)
            {
                if (this.rbAll.Checked)
                {
                    List<ExpressionObject> param = new List<ExpressionObject>();
                    param.Add(new ExpressionObject(InfoInvoices.INVPAYMENTSTATUS,1));

                    DataTable dtNursePayment = objConntroler.GetData("vw_NursePayment", param);

                    if (dtNursePayment == null || dtNursePayment.Rows.Count == 0)
                    {
                        this.dataGrid1.TableStyles.Clear();
                        this.dataGrid1.DataSource = null;
                    }
                    else
                    {
                        DataSet dsNursePayment = new DataSet();
                        dsNursePayment.Tables.Add(dtNursePayment);
                        this.dataGrid1.TableStyles.Clear();
                        this.dataGrid1.DataSource = null;
                        this.dataGrid1.DataSource = dsNursePayment;
                        this.dataGrid1.SetDataBinding(dsNursePayment, "vw_NursePayment");
                        this.loadDataGrid(this.dataGrid1);
                    }
                }
                else if (this.tbKeySearch.Text == "")
                {
                    MessageBox.Show("Vui lòng nhập từ khóa để tìm kiếm!", "Lỗi tìm kiếm", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    List<ExpressionObject> param = new List<ExpressionObject>();
                    param.Add(new ExpressionObject(InfoInvoices.INVPAYMENTSTATUS, 1));

                    DataTable dtNursePayment;
                    if (this.rbID.Checked)
                    {
                        param.Add(new ExpressionObject(InfoPatients.PATID, this.tbKeySearch.Text));
                        dtNursePayment = objConntroler.GetData("vw_NursePayment", param);
                    }
                    else
                    {
                        param.Add(new ExpressionObject(InfoPatients.PATNAME, this.tbKeySearch.Text, ExpressionObject.eSelectOperator.LikeAll));
                        dtNursePayment = objConntroler.GetData("vw_NursePayment", param);
                    }

                    if (dtNursePayment == null || dtNursePayment.Rows.Count == 0)
                    {
                        this.dataGrid1.TableStyles.Clear();
                        this.dataGrid1.DataSource = null;
                        MessageBox.Show("Không tìm thấy bệnh nhân ", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    else
                    {
                        DataSet dsNursePayment = new DataSet();
                        dsNursePayment.Tables.Add(dtNursePayment);
                        this.dataGrid1.DataSource = dsNursePayment;
                        this.dataGrid1.SetDataBinding(dsNursePayment, "vw_NursePayment");
                        this.dataGrid1.ReadOnly = true;
                        this.dataGrid1.TableStyles.Clear();
                        this.loadDataGrid(this.dataGrid1);
                    }
                }
            }
            else if (this.rbAll.Checked)
            {
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoInvoices.INVPAYMENTSTATUS, 0));

                DataTable dtNursePayment = objConntroler.GetData("vw_NursePayment", param);
                if (dtNursePayment == null || dtNursePayment.Rows.Count == 0)
                {
                    this.dataGrid2.TableStyles.Clear();
                    this.dataGrid2.DataSource = null;
                }
                else
                {
                    DataSet dsNursePayment = new DataSet();
                    dsNursePayment.Tables.Add(dtNursePayment);
                    this.dataGrid2.TableStyles.Clear();
                    this.dataGrid2.DataSource = null;
                    this.dataGrid2.DataSource = dsNursePayment;
                    this.dataGrid2.SetDataBinding(dsNursePayment, "vw_NursePayment");
                    this.loadDataGrid(this.dataGrid2);
                }
            }
            else if (this.tbKeySearch.Text == "")
            {
                MessageBox.Show("Vui lòng nhập từ khóa để tìm kiếm!", "Lỗi tìm kiếm", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                List<ExpressionObject> param = new List<ExpressionObject>();
                param.Add(new ExpressionObject(InfoInvoices.INVPAYMENTSTATUS, 0));

                DataTable dtNursePayment;

                if (this.rbID.Checked)
                {
                    param.Add(new ExpressionObject(InfoPatients.PATID, this.tbKeySearch.Text));
                    dtNursePayment = objConntroler.GetData("vw_NursePayment", param);
                }
                else
                {
                    param.Add(new ExpressionObject(InfoPatients.PATNAME, this.tbKeySearch.Text, ExpressionObject.eSelectOperator.LikeAll));
                    dtNursePayment = objConntroler.GetData("vw_NursePayment", param);
                }

                if (dtNursePayment == null || dtNursePayment.Rows.Count == 0)
                {
                    this.dataGrid2.TableStyles.Clear();
                    this.dataGrid2.DataSource = null;
                    MessageBox.Show("Không tìm thấy bệnh nhân", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else
                {
                    DataSet dsNursePayment = new DataSet();
                    dsNursePayment.Tables.Add(dtNursePayment);
                    this.dataGrid2.TableStyles.Clear();
                    this.dataGrid2.DataSource = null;
                    this.dataGrid2.DataSource = dsNursePayment;
                    this.dataGrid2.SetDataBinding(dsNursePayment, "vw_NursePayment");
                    this.loadDataGrid(this.dataGrid2);
                }
            }
        }

        private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.dataGrid1.DataSource != null)
            {
                DataSet dataSource = (DataSet)this.dataGrid1.DataSource;
                if (MessageBox.Show("Bạn có muốn xem hóa đơn của bệnh nhân này không?", "Xem hóa Đơn", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.clrISN = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 0].ToString();
                    this.patName = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 2].ToString();
                    this.patID = this.dataGrid1[this.dataGrid1.CurrentRowIndex, 1].ToString();
                    this.index = this.dataGrid1.CurrentRowIndex;
                    frmLapHoaDonYTa ta = new frmLapHoaDonYTa(this.patID, this.patName, this.clrISN);
                    ta.ShowInTaskbar = false;
                    ta.ShowDialog(this);
                    if (UserInformation.rowUpdated)
                    {
                        dataSource.Tables[0].Rows.RemoveAt(this.index);
                        this.bmb = this.BindingContext[this.dataGrid1.DataSource, this.dataGrid1.DataMember];
                    }
                }
            }
        }

        private void dataGrid2_MouseUp(object sender, MouseEventArgs e)
        {
            if (this.dataGrid2.DataSource != null)
            {
                DataSet dataSource = (DataSet)this.dataGrid2.DataSource;
                if (MessageBox.Show("Bạn có muốn xem lại hóa đơn của bệnh nhân này không?", "Xem hóa đơn", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.ISN = dataSource.Tables[0].Rows[this.dataGrid2.CurrentRowIndex][1].ToString();
                    string str = dataSource.Tables[0].Rows[this.dataGrid2.CurrentRowIndex][InfoClinicalRecord.CLRISN].ToString();

                    List<ExpressionObject> param = new List<ExpressionObject>();
                    param.Add(new ExpressionObject(InfoPatients.PATID, this.ISN));
                    param.Add(new ExpressionObject(InfoClinicalRecord.CLRISN, str));
                    DataTable dtNursePayment = objConntroler.GetData("vw_NursePayment", param);
                    if (dtNursePayment != null && dtNursePayment.Rows.Count > 0)
                    {
                        DataRow dr = dtNursePayment.Rows[0];

                        this.clrISN = dr[InfoClinicalRecord.CLRISN].ToString();
                        this.patName = dr[InfoPatients.PATNAME].ToString();
                        this.patID = dr[InfoPatients.PATID].ToString();
                        string str2 = dr[InfoInvoices.INVMEDICINEPAYMENT].ToString();
                        string str3 = dr[InfoInvoices.INVEXAMINATIONPAYMENT].ToString();
                        string str4 = dr[InfoInvoices.INVMEDICINESKILLPAYMENT].ToString();
                        string str5 = dr[InfoInvoices.INVSURGERYPAYMENT].ToString();
                        string str6 = dr[InfoInvoices.INVISN].ToString();

                        frmHoaDonYTa ta = new frmHoaDonYTa(this.patName, this.patID, this.clrISN, str2, str3, str4, str5, str6);
                        ta.ShowInTaskbar = false;
                        ta.ShowDialog(this);
                    }
                }
            }
        }

        private void frmBenhAnChuaTinhTien_Load(object sender, EventArgs e)
        {
            this.btSearch_Click(sender, e);
        }

        public bool IsNumber(string Number)
        {
            try
            {
                long.Parse(Number);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void loadDataGrid(DataGrid dg)
        {
            DataGridTableStyle table = new DataGridTableStyle();
            table.MappingName = "vw_NursePayment";
            table.AlternatingBackColor = Color.AliceBlue;
            table.HeaderForeColor = Color.Maroon;
            table.GridLineColor = Color.CornflowerBlue;
            DataGridTextBoxColumn column = new DataGridTextBoxColumn();
            column.MappingName = InfoClinicalRecord.CLRISN;
            column.HeaderText = "";
            column.Width = 0;
            table.GridColumnStyles.Add(column);
            DataGridTextBoxColumn column2 = new DataGridTextBoxColumn();
            column2.MappingName = InfoPatients.PATID;
            column2.HeaderText = "Số ID";
            column2.Width = 60;
            table.GridColumnStyles.Add(column2);
            DataGridTextBoxColumn column3 = new DataGridTextBoxColumn();
            column3.MappingName = InfoPatients.PATNAME;
            column3.HeaderText = "Tên Bệnh Nhân";
            column3.Width = 210;
            table.GridColumnStyles.Add(column3);
            DataGridTextBoxColumn column4 = new DataGridTextBoxColumn();
            column4.MappingName = InfoPatients.PATBIRTHDATE;
            column4.HeaderText = "Ngày Sinh";
            column4.Width = 100;
            table.GridColumnStyles.Add(column4);
            DataGridTextBoxColumn column5 = new DataGridTextBoxColumn();
            column5.MappingName = InfoClinicalRecord.CLREXAMINATIONDATE;
            column5.HeaderText = "Ngày Lập Bệnh Án";
            column5.Width = 140;
            column5.NullText = string.Empty;
            table.GridColumnStyles.Add(column5);
            DataGridTextBoxColumn column6 = new DataGridTextBoxColumn();
            column6.MappingName = InfoClinicalRecord.CLREXAMINATIONTIMES;
            column6.HeaderText = "Lần Khám";
            column6.Width = 120;
            column6.NullText = string.Empty;
            table.GridColumnStyles.Add(column6);
            DataGridTextBoxColumn column7 = new DataGridTextBoxColumn();
            column7.MappingName = InfoClinicalRecord.CLRREXAMINATIONTIMES;
            column7.HeaderText = "Lần Tái Khám";
            column7.Width = 120;
            column7.NullText = string.Empty;
            table.GridColumnStyles.Add(column7);
            dg.TableStyles.Add(table);
            table.AllowSorting = false;
            this.bmb = this.BindingContext[dg.DataSource, dg.DataMember];
        }

        private void tbGoTo_TextChanged(object sender, EventArgs e)
        {
            DataSet dataSource = new DataSet();
            if (this.tbControl.SelectedIndex == 0)
            {
                dataSource = (DataSet)this.dataGrid1.DataSource;
            }
            else
            {
                dataSource = (DataSet)this.dataGrid2.DataSource;
            }
            if (this.IsNumber(this.tbGoTo.Text.ToString()))
            {
                for (int i = 0; i < dataSource.Tables["vw_NursePayment"].Rows.Count; i++)
                {
                    if (dataSource.Tables["vw_NursePayment"].Rows[i][1].ToString() == this.tbGoTo.Text.ToString())
                    {
                        this.bmb.Position = i;
                        break;
                    }
                }
            }
            else
            {
                this.tbGoTo.SelectAll();
            }
        }

        private void tbKeySearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                this.btSearch_Click(sender, e);
            }
        }

    }
}